home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1943 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What should be returned?
  5. Date: Wed, 17 Jan 96 22:33:53 GMT
  6. Organization: none
  7. Message-ID: <821918033snz@genesis.demon.co.uk>
  8. References: <4dj8pv$cjd@eng_ser1.erg.cuhk.hk> <Crawford.821899744@mariner>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <Crawford.821899744@mariner> crawford@iac.net "CRAWFORD" writes:
  15.  
  16. >phsung@cs.cuhk.hk (the CAReLess boy) writes:
  17. >>Hi, all,
  18. >>       It's said that the function main must return an integer value
  19. >>but I just don't know what value should be returned.
  20.  
  21. The C language defines 3 values which may be passed portably to exit()
  22. and hence returned from an initial invocation of main:
  23.  
  24. 0 - indicates a successful return
  25.  
  26. EXIT_SUCCESS - indicates a successful return
  27.  
  28. EXIT_FAILURE - indicates an unsuccessful return
  29.  
  30. EXIT_SUCCESS and EXIT_FAILURE are defined (along with exit() in stdlib.h.
  31.  
  32. While 0 and EXIT_SUCCESS mean the same thing, EXIT_SUCCESS doesn't
  33. have to be defined as 0 - there could be more than one value indicating
  34. success. Your implementation can define (or allow you to use) other values
  35. but these values can be used across all ANSI conforming implementations.
  36.  
  37. >>  Also, if I quit
  38. >>main by exit(), what's the use of the return value?
  39. >>       Any help?
  40.  
  41. No use at all. If code can never reach the end of a function in C there
  42. is no requirement for the function to end in a return statement. However a
  43. C function must always be defined with an equivalent return type to that
  44. which the caller expects (for example so that the correct call sequence for
  45. that function is generated). A C runtime system is entitled to expect to
  46. call a main function that returns int so, to be portable, you must provide
  47. it with one. Not returning from a function gives you no license to change the
  48. return type of the function, unless you do so in both the caller and callee.
  49. At program startup you have no control over the caller.
  50.  
  51. >        exit() also causes a value to be returned from main, the POSIX
  52. >prottype for exit() is:
  53. >
  54. >void exit(int status);
  55.  
  56. POSIX simply adopted the ANSI C prototype for exit().
  57.  
  58. >        In the UNIX world (and, I believe, DOS), the standard is to
  59. >return zero on success and non-zero on a failure. That's what the
  60. >macros evaluate to, but I'd use the macros anyway, since they're easy
  61. >to understand.
  62.  
  63. Zero is guaranteed to be transformed by the implementation in whatever way
  64. necessary to generate a successful termination condition. There are
  65. no guarantees for non-zero returns e.g. in VMS 1 is a success indication.
  66. EXIT_FAILURE is the only portable way of generating a failure indication.
  67.  
  68. -- 
  69. -----------------------------------------
  70. Lawrence Kirby | fred@genesis.demon.co.uk
  71. Wilts, England | 70734.126@compuserve.com
  72. -----------------------------------------
  73.